package com.hi.hiparty.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class TimeUtils {

	public static final String DEFUALT_DATE_FORMAT_STRING = "yyyy-MM-dd";

	public static final String DEFUALT_TIME_FORMAT_STRING = "HH:mm";

	public static final String DEFUALT_DATETIME_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";

	public static SimpleDateFormat dateFormat = new SimpleDateFormat(DEFUALT_DATE_FORMAT_STRING);

	public static SimpleDateFormat timeFormat = new SimpleDateFormat(DEFUALT_TIME_FORMAT_STRING);

	public static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DEFUALT_DATETIME_FORMAT_STRING);

	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}

	public static String formatTime(Date date) {
		return timeFormat.format(date);
	}

	public static String formatDateTime(Date date) {
		return dateTimeFormat.format(date);
	}

	public static Date parseDateTime(String date) throws ParseException {
		return dateTimeFormat.parse(date);
	}

	public static Date parseDateAndTime(String dateString, String timeString) {
		if (StringUtils.isBlank(dateString)) {
			return null;
		}
		if (StringUtils.isBlank(timeString)) {
			timeString = "00:00";
		}
		try {
			return dateTimeFormat.parse(dateString + " " + timeString);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String combineDateAndTime(String dateString, String timeString) {
		if (StringUtils.isBlank(dateString)) {
			return null;
		}
		if (StringUtils.isBlank(timeString)) {
			timeString = "00:00";
		}
		return dateString + " " + timeString;
	}

	public static Date getDefaultPartyStartTime() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 10);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTime();
	}

	public static Date getDefaultPartyEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 2);
		cal.set(Calendar.HOUR_OF_DAY, 10);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTime();
	}

	public static Date add(Date date, int field, int value) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, value);
		return cal.getTime();
	}

	public static Date tomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}

	public static Date dayAfterTomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}
}
